package com.stars.home.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.cardview.widget.CardView
import androidx.lifecycle.Observer
import cn.bingoogolapple.bgabanner.BGABanner
import coil.load
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.activity.*
import com.stars.home.adapter.HomeDriverAdapter
import com.stars.home.base.MBRFragment
import com.stars.home.config.ConfigRx
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.FragmentHomeDriverBinding
import com.stars.home.inte.ChoiceData
import com.stars.home.model.CompreBean
import com.stars.home.model.IndexTop
import com.stars.home.model.User
import com.stars.home.utils.PopuTipsUtils
import com.stars.home.vmodel.HomeDriverVModel
import com.tencent.mmkv.MMKV
import com.trailer.base.net.IResp
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch

/**
 * 司机首页
 *on 2021/5/9
 */
class HomeDriverFragment : MBRFragment<HomeDriverVModel, HomeDriverAdapter, FragmentHomeDriverBinding>() {


    var type = 1

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.fl = binding.fl

        vm.getHeadInfo()
        setHead()
        getLocation()

        vm.bean.observe(this, Observer {
            if (null == it) {
                return@Observer
            }
            if (null != it.ad && it.ad!!.isNotEmpty()) {
                Log.e("-shy-", "item.image=:0000000000 ")

                initBanner(it.ad!!)
            }

        })


    }


    fun getLocation() {
        //获取司机的当前位置-去定位一次




    }


    private fun setHead() {
        //头部详情


        binding?.imgTime?.onClick {
            //选择时间
            Log.e("-shy-", "1111 ")
            UiSwitch.singleRes(this, ChoseTimeActivity::class.java, MIntentAction.REQUEST_CODE_ONE)

        }
        binding?.llStartCity?.onClick {
            //选择开始城市
            type = 1
            UiSwitch.bundleRes(this, AreaSelectActivity::class.java,
                    B().putInt("chose", 0).ok(),
                    MIntentAction.REQUEST_CODE_TWO)
        }

        binding?.llEndCity?.onClick {
            //选择开始城市
            type = 2
            UiSwitch.bundleRes(this, AreaSelectActivity::class.java,
                    B().putInt("chose", 0).ok(),
                    MIntentAction.REQUEST_CODE_TWO)

        }
        binding?.llToLook?.onClick {
            //接单
            UiSwitch.single(context, MsgActivity::class.java)

        }
        binding.imgDeleteStart.onClick {
            vm.start_city_name.value = ""
            onRefresh()

        }
        binding.imgDeleteEnd.onClick {
            vm.end_city_name.value = ""

            onRefresh()
        }

        binding.butChange.onClick {
            //退出
            PopuTipsUtils.show(context(), "确定切换身份么？", binding.butChange, object : ChoiceData {
                override fun onRestuse(id: String?) {


                    if (null != User.get()) {

                    }

                    //只清空这些数据
                    MMKV.defaultMMKV().putString("item", "")
                    MMKV.defaultMMKV().putString("to","")
                    MMKV.defaultMMKV().putString("token","")
//                    MMKVManager.instance().clearAll()
                    UiSwitch.single_new_task_with_clear_task(context, LoginActivity::class.java)

                    (context as Activity).finish()

                }
            })


        }


    }

    override fun listeners() {
        super.listeners()

        binding.llToLook.onClick {
            UiSwitch.single(context(), MsgActivity::class.java)

        }


    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (MIntentAction.REQUEST_CODE_ONE == requestCode && resultCode == Activity.RESULT_OK) {//时间

            val bundle = data!!.extras
            if (null != bundle) {

                vm.start_time.value = bundle.getString("startDay")
                vm.end_time.value = bundle.getString("endDay")

                onRefresh()


            }
            return
        }
        if (MIntentAction.REQUEST_CODE_TWO == requestCode && resultCode == Activity.RESULT_OK) {//城市

            val bundle = data!!.extras
            if (null != bundle) {
                if (type == 1) {
                    vm.start_city_name.value = bundle.getString("cityname")!!
                } else {
                    vm.end_city_name.value = bundle.getString("cityname")!!


                }


                onRefresh()

            }
        }


    }


    fun initBanner(bean: List<IndexTop.AdBean>) {

        var list = ArrayList<String>()


        for (item in bean) {

            Log.e("-shy-", "item.image=: " + item.image)
            list.add(item.image)
        }

        if (list.size > 1) {
            binding?.banner?.setAutoPlayAble(true)
        } else {
            binding?.banner?.setAutoPlayAble(false)
        }

        binding?.banner?.setAdapter(BGABanner.Adapter<CardView, String?> { banner, itemView, model, position -> //图片布局
            val simpleDraweeView = itemView.findViewById<ImageView>(R.id.sdv_item_fresco_content)
            simpleDraweeView.load(model)
        })
        binding?.banner?.setData(R.layout.item_fresco, list, null)
        binding?.banner?.setAutoPlayInterval(6000)
        binding?.banner?.setDelegate { banner, itemView, model, position ->

            //todo： var ad = beanlist[position]

        }

    }


    override fun onListReceive(res: IResp<*>) {
        super.onListReceive(res)

        if (null != res) {

            var list = res.data() as List<CompreBean>
            if (list.isEmpty() && vm.repository().page == 1) {

                binding.listEmpty.visibility = View.VISIBLE
            } else {
                binding.listEmpty.visibility = View.GONE
            }

        }
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeDriverBinding {
        return FragmentHomeDriverBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): HomeDriverAdapter {
        return HomeDriverAdapter(vm)
    }

    override fun vmClass(): Class<HomeDriverVModel> {
        return HomeDriverVModel::class.java
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true


    @Subscribe(tags = [Tag(ConfigRx.REFRESH_DRIVER_HOME)])
    fun setRefreshHome(position: String) {
        if (!TextUtils.isEmpty(position)) {

            var index = position.toInt()
            if (adapter().data.size > index) {
                //移除
                adapter().removeAt(index)
            }

        }

    }


    @Subscribe(tags = [Tag(ConfigRx.REFRESH_MSG)])
    fun setRefreshMsg(position: String) {
        if (!TextUtils.isEmpty(position)) {
            //刷新通知消息
            vm.getHeadInfo()
        }

    }


}
